<template>
  <div class="register-area" data-info="建档申领">
    <el-button class="next" @click="handleNext" type="primary">
      下一步{{ stepInfo }}
    </el-button>

    <div class="step-comps-area" data-info="具体的业务">
      <component
        :is="curCompName"
        @handleSure="handleSure"
        @postUserMsg="postUserMsg"
        @cancelBtn="cancelBtn"
        @sureBtn="sureBtn"
        :SelectTheCardsetupType="SelectTheCardsetupType"
        @handlePatientInfoSure="handlePatientInfoSure"
        :mobileNum="mobileNum"
        :isUseReq="false"
        :FromFileApply="true"
      />
    </div>
    <!--底部操作-->
    <PatientNumberOperate
      @handleBack="handlePre"
      :key="widget"
      :time="120000"
    />
  </div>
</template>

<script>
import PatientNumberOperate from "../comps/patientNumberOperate";
import SelectTheCardIssuer from "@sc/DRCbiz/comps/Select-the-card-issuer/index.vue";
import CardType from "@sc/DRCbiz/comps/cardType/index.vue";
import MedInsCode from "@sc/DRCbiz/comps/medInsCode/index.vue";
import PatientInfo from "@sc/DRCbiz/comps/patientInfo/index.vue";
import AddMobileNumber from "@sc/DRCbiz/comps/Add-mobile-number/index.vue";
// import AddVerificationCode from "@sc/DRCbiz/comps/Add-verification-code/index.vue";
import FilingSuccessful from "@sc/DRCbiz/comps/filing-successful/index.vue";
import { v4 as uuid } from "uuid";
import { parseUserMsg } from '@sc/DRCbiz/comps/Add-verification-code/pare-use-id.js'
import { AndroidToWeb, buildParentApi } from "@/utils/designer/util";
let buildParent = buildParentApi();
export default {
  name: "FileApply",

  componentName: "FileApply",

  components: {
    PatientNumberOperate,
    SelectTheCardIssuer,
    CardType,
    MedInsCode,
    PatientInfo,
    AddMobileNumber,
    // AddVerificationCode,
    FilingSuccessful,
  },

  data() {
    return {
      widget: uuid(),
      steps: 1,
      curCompName: "CardType",
      compsOptions: {
        1: "SelectTheCardIssuer",
        2: "CardType",
        3: "MedInsCode",
        4: "PatientInfo",
        5: "AddMobileNumber",
        // 6: "AddVerificationCode",
        6: "FilingSuccessful",
      },
      SelectTheCardsetupType: "1",
      mobileNum: '',
    };
  },

  computed: {
    shownOperate() {
      return this.steps !== 1;
    },
    patientInfo() {
      return this.$store.getters["DRCbiz/getPatientInfo"];
    },
    // 步骤
    stepInfo() {
      return this.$store.getters["DRCbiz/getStepsInfo"];
    },
    //选中的卡片信息
    cardTypeInfo() {
      return this.$store.getters['DRCbiz/getCardTypeInfo']
    },
  },

  mounted() {
    this.$store.dispatch("DRCbiz/setCTDTimeInfo", 1200000);
  },

  methods: {
    // 患者信息-->获取门诊类型
    handlePatientInfoSure() {
      let stepInfo = parseInt(this.stepInfo);
      this.$store.dispatch("DRCbiz/setStepsInfo", ++stepInfo);
    },
    /**
     * @name 确定
     */
    sureBtn(mobile) {
      // let stepInfo = parseInt(this.stepInfo);
      // this.$store.dispatch("DRCbiz/setStepsInfo", ++stepInfo);
      this.mobileNum = mobile

      let devInfo = JSON.parse(localStorage.getItem("deviceInfo"));
      let parseInfo = parseUserMsg(this.cardTypeInfo.cardInfo);
      let cardId = this.cardTypeInfo.id;
      let patientType = cardId === 10 || cardId == 29 ? "医保" : "自费";
      let params = {
        Request: {
          deviceIp: devInfo.screenIp,
          flowId: uuid(),
          timestamp: this.$day().valueOf(),
          terminalNo: devInfo?.screenId, // 自助机设备终端	C	可空	根据医院实际情况
          method: "CreateCardAddPosit",
          // idNo:'',
          idType: this.cardTypeInfo.id, // 证件类别
          // cardNo:'',
          // cardType:'',
          setupType: this.SelectTheCardsetupType, // 建档类型	N	可空	1、本人 2、儿童 默认1
          patientType,
          // name:'',
          // sex:'',
          // birthday:'',
          // address:'',
          // phone:'',
          phone: this.mobileNum,
          // pwd: this.patientInfo.result.cardNo,
          pwd: parseInfo.cardNo,
          accountNo: "", // 交易账户	C	不可空	根据不同支付方式传入如院内账号就是卡号
          cash: "0.00", // cash	金额	C	不可空	发卡费
          // ...this.patientInfo.result,
          ...parseInfo,
        },
      };
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod(
        "axiosRep",
        paramsInfo,
        "CreateCardAddPosit"
      );
    },
    /**
     * @name 手机号取消
     */
    cancelBtn() {
      this.steps = --this.steps;
    },
    /**
     * @name 所选建档类型
     */
    postUserMsg(row) {
      this.SelectTheCardsetupType = row.value;
      // this.steps = ++this.steps;
      let stepInfo = parseInt(this.stepInfo);
      this.$store.dispatch("DRCbiz/setStepsInfo", ++stepInfo);
    },

    handleSure(useMsg) {
      console.log("sure1", useMsg, this.patientInfo);
      // <Request>
      //   <deviceIp>172.17.43.202</deviceIp>
      //   <flowId>JWS30320250206081219385</flowId>
      //   <timestamp>20250206081219385</timestamp>
      //   <terminalNo>JWS303</terminalNo>
      //   <method>QueryCostSummary</method>
      //   <patientId>4026835</patientId>
      // </Request>
      const { patientId } = this.patientInfo;
      let params = {
        Request: {
          deviceIp: "",
          flowId: "",
          timestamp: "",
          terminalNo: "",
          method: "QueryCostSummary",
          patientId,
        },
      };
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod(
        "axiosRep",
        paramsInfo,
        "QueryCostSummary"
      );
    },
    async handleNext() {
      let stepInfo = parseInt(this.stepInfo);
      this.widget = uuid();
      if (stepInfo === 6) {
        await this.$store.dispatch("DRCbiz/setStepsInfo", 1);
      } else {
        await this.$store.dispatch("DRCbiz/setStepsInfo", ++stepInfo);
      }
    },

    // 返回上一层&首页
    // handlePre() {
    //   clearInterval(this.timer);
    //   if (this.steps === 1) {
    //     this.$router.push({
    //       path: `/DRCbizList`,
    //     });
    //   } else {
    //     this.steps = --this.steps;
    //   }
    // },
    // 返回上一层&首页
    async handlePre() {
      this.widget = uuid();
      let stepInfo = parseInt(this.stepInfo);
      if (stepInfo === 1) {
        await this.$router.push({
          path: `/DRCbizList`,
        });
      } else {
        await this.$store.dispatch("DRCbiz/setStepsInfo", --stepInfo);
      }
      switch (parseInt(this.stepInfo)) {
        case 2:
          let cardTypeInfo = this.cardTypeInfo;
          Reflect.set(cardTypeInfo, "cardNo", null);
          await this.$store.dispatch("DRCbiz/setCardTypeInfo", cardTypeInfo);
          buildParent.executeParentMethod("openScan");
          break;
      }
    },
  },

  watch: {
    // 步距
    stepInfo: {
      handler: function (val) {
        const self = this;
        self.widget = uuid();
        let OPTypeInfo = self.OPTypeInfo;
        let cardTypeInfo = self.cardTypeInfo;
        if ( cardTypeInfo && val === 3) {
          // 电子医保特殊处理
          if (parseInt(cardTypeInfo?.id) === 29) {
            buildParent.executeParentMethod("MICode");
          } else {
            buildParent.executeParentMethod("openScan");
          }
        } else {
          buildParent.executeParentMethod("closeScan");
        }
        
        self.curCompName = Reflect.get(self.compsOptions, val);

      },
      deep: true,
      immediate: true,
    },
  },
};
</script>

<style scoped lang="scss">
@import "./assets/style";
</style>